﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using ElectronicCommerce.Framework.Collections;
using ElectronicCommerce.Framework.Configuration.Attributes;
using ElectronicCommerce.Framework.Diagnostics;
using ElectronicCommerce.Framework.Utility;

namespace ElectronicCommerce.Framework.ServiceModel.Configuration
{
    /// <summary>
    /// 
    /// </summary>
    [ConfigFile("ServiceModel/Services/*.config")]
    [XmlRoot("serviceConfig", Namespace = NamespaceConst.NAMESPACE)]
    public class ServiceConfig
    {
        private ServiceElementCollection services;

        /// <summary>
        /// 
        /// </summary>
        [XmlElement("service")]
        public ServiceElementCollection Services
        {
            get { return services; }
            set { services = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <param name="serviceName"></param>
        /// <returns></returns>
        public ServiceElement GetMixServiceElement(Type serviceType, string serviceName)
        {
            ServiceElement serviceElement = null;
            ServiceConfig serviceConfig = ServiceModelConfigHelper.ServiceConfig;

            if (serviceConfig != null)
            {
                serviceElement = GetServiceElement(serviceType, serviceName);
                if (serviceElement == null)
                {
                    KeyedCollection<ServiceElement> serviceElements = GetServiceElements(serviceType);
                    if (serviceElements.Count > 0)
                    {
                        serviceElement = serviceElements[0];
                    }
                }
            }

            return serviceElement;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceName"></param>
        /// <returns></returns>
        public ServiceElement GetServiceElement(Type serviceType, string serviceName)
        {
            Check.Argument.IsNotNull("serviceType", serviceType);

            string serviceTypeName = serviceType.FullName;
            if (StringHelper.IsEmpty(serviceName))
            {
                serviceName = serviceType.Name;
            }

            string serviceKey = ServiceElement.GenerateKey(serviceTypeName, serviceName);

            return Services[serviceKey];
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <returns></returns>
        public KeyedCollection<ServiceElement> GetServiceElements(Type serviceType)
        {
            Check.Argument.IsNotNull("serviceType", serviceType);

            KeyedCollection<ServiceElement> typeServiceElements = null;
            services.TypedServiceElements.TryGetValue(serviceType.FullName, out typeServiceElements);
            return typeServiceElements ?? new KeyedCollection<ServiceElement>();
        }
    }
}
